/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.client.view;

import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import com.agenda.PrincipalCliente;
import com.agenda.model.Compromisso;
import com.agenda.model.Convite;
import com.agenda.util.StringUtils;

/**
 *
 * @author Murilo
 */
public class CompromissoListCellRenderer extends JLabel implements ListCellRenderer {

	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

		StringBuilder valueStr = new StringBuilder();
		Color cor = list.getBackground();
		if (value instanceof Compromisso) {
			Compromisso comp = (Compromisso) value;
			valueStr.append(StringUtils.toStr(comp.getDataInicial(), "dd/MM/yyyy"));
			valueStr.append(" - ").append(comp.getNome());
			valueStr.append(" - ").append(comp.getProprietario().getLoginEmail());
			if (comp.getDescricao() != null && !comp.getDescricao().isEmpty())
				valueStr.append(" - ").append(comp.getDescricao());

			Convite convite = comp.getConvite(PrincipalCliente.getUsuarioLogado());
			if (convite != null) {
				final int resposta = convite.getResposta();
				switch (resposta) {
					case Convite.NAO_RESPONDIDO:
						cor = Color.pink;
						break;
					case Convite.ACEITO:
						cor = Color.green;
						break;
					case Convite.RECUSADO:
						cor = Color.magenta;
						break;
				}
			} else {
				setBackground(list.getBackground());
			}

			if (comp.isConfirmado())
				setIcon(new ImageIcon(getClass().getResource("/com/agenda/client/view/imagens/check2.png")));
			else
				setIcon(new ImageIcon(getClass().getResource("/com/agenda/client/view/imagens/question_and_answer.png")));

		} else {
			valueStr.append(value);
		}

		setText(valueStr.toString());

		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(Color.RED);
		} else {
			setForeground(Color.BLACK);
			setBackground(cor);
		}

		setEnabled(list.isEnabled());
		setFont(list.getFont());

		setOpaque(true);

		//setSize(list.setFixedCellHeight(arg0, arg1)e, height);
		return this;
	}
}
